package vip.xiaonuo.device.modular.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.device.core.enums.Constants;
import vip.xiaonuo.device.modular.entity.ApiLog;
import vip.xiaonuo.device.modular.entity.BizCabinetGroup;
import vip.xiaonuo.device.modular.entity.SysUser;
import vip.xiaonuo.device.modular.mapper.CabinetGroupMapper;
import vip.xiaonuo.device.modular.mapper.RelationMapper;
import vip.xiaonuo.device.modular.mapper.UserMapper;
import vip.xiaonuo.device.vo.Result;

/**
 * @author 墨塵
 * @since 2023/6/15
 */
@Service("deviceUserCheckService")
public class DeviceUserCheckServiceImpl extends DeviceBaseService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CabinetGroupMapper cabinetGroupMapper;

    @Autowired
    private RelationMapper relationMapper;

    @Override
    protected Integer getReqType() {
        return Constants.CHECKUSER;
    }

    @Override
    protected Result checkData(JSONObject param) {
        String cardNum = param.getString("cardnum");
        String cabinetIdentify = param.getString("cabinetIdentify");
        if (StringUtils.isBlank(cardNum)) {
            return Result.fail("卡号cardNum信息缺失");
        }
        if (StringUtils.isBlank(cabinetIdentify)) {
            return Result.fail("组柜设备硬件标识cabinetIdentify信息缺失");
        }
        return Result.ok();
    }

    @Override
    protected Result handle(JSONObject param) {
        String cardNum = param.getString("cardnum");
        String cabinetIdentify = param.getString("cabinetIdentify");
        SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserNo, cardNum));
        if (null == user) {
            return Result.fail(12001, "用户信息不存在");
        }
        BizCabinetGroup cabinetGroup = cabinetGroupMapper.getInfoByIdentify(cabinetIdentify);
        if (null == cabinetGroup) {
            return Result.fail(12001, "柜组信息不存在");
        }
        Boolean isManager = this.checkManager(user.getId(), cabinetGroup.getId());
        JSONObject result = new JSONObject();
        result.put("name", user.getName());
        result.put("ismanager", isManager);
        return Result.ok(result);
    }

    @Override
    protected void handleLog(ApiLog log, JSONObject param) {
        String cardNum = param.getString("cardnum");
        SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getIdCardNumber, cardNum));
        if (user == null) return;
        log.setUserNo(cardNum);
        log.setUserId(user.getId());
    }

    private Boolean checkManager(String userId, String cabGroupId) {
        //查询用户是否为柜组管理员
        int groupCount = relationMapper.countForGroup(cabGroupId, userId);
        if (groupCount > 0) {
            return true;
        } else {
            int cabCount = relationMapper.countForCabByGroupId(cabGroupId, userId);
            if (cabCount > 0) {
                return true;
            } else {
                int doorCount = relationMapper.countForDoorByGroupId(cabGroupId, userId);
                if (doorCount > 0) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
}
